package org.bronycorps.mlptss.utils;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * Represents scrolling background.
 * 
 */
public class BackgroundSprite extends Sprite {

	private Float mainSpeed;
	private final int maxWidth;

	private float scrollTimer;

	private float speed;

	/**
	 * Create scrolling background sprite.
	 * 
	 * @param texture
	 *            The texture to scroll.
	 */
	public BackgroundSprite(Texture texture, float speed, Float mainSpeed) {
		super(texture);
		this.mainSpeed = mainSpeed;

		this.maxWidth = texture.getWidth();

		this.speed = speed;

		this.scrollTimer = 0.0f;

		texture.setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
	}

	public void act(float delta) {
		this.scrollTimer += (delta * this.speed * this.mainSpeed);
		if (this.scrollTimer > this.maxWidth) {
			this.scrollTimer = 0.0f;
		}
	}

	/**
	 * Scroll the background across the frame duration.
	 * 
	 */
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {

		float scaled = this.scrollTimer / this.maxWidth;

		setU(scaled);
		setU2(scaled + 1);

		super.draw(batch);
	}

	public Float getMainSpeed() {
		return this.mainSpeed;
	}

	public void setMainSpeed(Float mainSpeed) {
		this.mainSpeed = mainSpeed;
	}

}